package com.hmdp;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmdp.entity.Shop;
import com.hmdp.entity.User;
import com.hmdp.service.impl.ShopServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@SpringBootTest
public class RedisStringTemplate {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ShopServiceImpl shopService;

    @Test
    void testString(){
        stringRedisTemplate.opsForValue().set("name","张三");
        String name = stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
    }

    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Test
    void testJson() throws JsonProcessingException {
        User user = new User("huge");
        String json = objectMapper.writeValueAsString(user);
        stringRedisTemplate.opsForValue().set("user:100",json);
        String json1 = stringRedisTemplate.opsForValue().get("user:100");
        User user1 = objectMapper.readValue(json1, User.class);
        System.out.println(user1);
    }

    @Test
    void testHash(){
        stringRedisTemplate.opsForHash().put("user:400","name","huge");
        stringRedisTemplate.opsForHash().put("user:400","age","25");
        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:400");
        Set<Object> keys = stringRedisTemplate.opsForHash().keys("user:400");
        List<Object> values = stringRedisTemplate.opsForHash().values("user:400");
        System.out.println(entries);
        System.out.println(keys+String.valueOf(values));
    }
    @Test
    void testSaveShop() throws InterruptedException {
        shopService.saveShop2Redis(1L,30L);
    }

    @Test
    void loadShopData(){
        List<Shop> shopList = shopService.list();
        //按照typeId分组得到两个店铺列表
        Map<Long, List<Shop>> map = shopList.stream().collect(Collectors.groupingBy(Shop::getTypeId));
        for (Map.Entry<Long, List<Shop>> entry : map.entrySet()) {
            Long typeId = entry.getKey();
            String key = "shop:geo:"+typeId;
            List<Shop> shops = entry.getValue();
            List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(shops.size());
            for (Shop shop : shops) {
                RedisGeoCommands.GeoLocation<String> geoLocation = new RedisGeoCommands.GeoLocation<>
                        (shop.getId().toString(),new Point(shop.getX(),shop.getY()));
                locations.add(geoLocation);
            }
            stringRedisTemplate.opsForGeo().add(key,locations);
        }
    }

    @Test
    void testHyperLogLog(){
        String[] values = new String[1000];
        int j =0;
        for (int i = 0; i <1000000; i++) {
            j = i%1000;
            values[j] = "user_id_" + i;
            if (j == 999){
                stringRedisTemplate.opsForHyperLogLog().add("hl22",values);
            }
        }
        Long size = stringRedisTemplate.opsForHyperLogLog().size("hl22");
        System.out.println(size);
    }
//    @Test
//    void testString1(){
//
//                // 步骤1：打印Java版本
//                System.out.println("Java版本: " + System.getProperty("java.version"));
//
//                // 步骤2：创建s1（确保是运行时动态拼接）
//                String s1 = new String("1") + new String("2");
//
//                // 步骤3：验证intern()的返回值
//                System.out.println("s1.intern() == s1: " + (s1.intern() == s1));
//
//                // 步骤4：创建s2
//                String s2 = "12";
//
//                // 步骤5：比较s1和s2
//                System.out.println("s1 == s2: " + (s1 == s2));
//
//
//    }
}
